package com.ouyeel.wlxer.base;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.widget.MyDiglog;
import com.ouyeel.wlxer.widget.MyToast;
import com.xys.libzxing.zxing.utils.ViewUtil;

import butterknife.ButterKnife;

/**
 * Created by rankaifeng on 2017/9/25.
 */

public abstract class BaseActivity extends AppCompatActivity {
    Dialog progressDlg;
    private TextView tvTitle;
    private ImageView imgMy;
    private RelativeLayout relLeftImage, relTitleTop;
    private TextView tvRight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            ViewUtil.initSystemBar(this, R.color.system_bar_bg);
        }
        setContentView(getLayout());
        ButterKnife.bind(this);
        defaultActionBar();
        initView();
    }


    public abstract int getLayout();

    public abstract void initView();

    /**
     * 显示左边图片 中间标题
     *
     * @param title
     */
    public void showTitleAndLeftImg(String title) {
        imgMy.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.GONE);
        showTitle(title);
    }

    /**
     * 显示中间标题 左边返回按钮
     *
     * @param title
     */
    public void showTitleAndLeftBack(String title) {
        relLeftImage.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.GONE);
        showTitle(title);
    }

    /**
     * 显示中间标题 左边返回按钮 右边文字
     *
     * @param title
     * @param rightText
     */
    public void showTitleLeftBackRight(String title, String rightText) {
        relLeftImage.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(rightText);
        showTitle(title);
    }

    private void showTitle(String title) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(title);
    }

    /**
     * 显示加载进度框
     *
     * @param msg
     */
    public void showProgress(String msg) {
        progressDlg = MyDiglog.createLoadingDialog(BaseActivity.this, msg, false);
    }

    /**
     * 隐藏进度框
     */
    public void hideProgress() {
        if (progressDlg != null && progressDlg.isShowing()) {
            progressDlg.dismiss();
        }
    }

    /**
     * 显示tost
     *
     * @param msg
     */
    public void showTost(String msg) {
        MyToast myToast = MyToast.makeText(BaseActivity.this, msg);
        myToast.setGravity(Gravity.CENTER, 0, 0);
        myToast.show();
    }

    private void defaultActionBar() {
        View actionbarView = getLayoutInflater().inflate(R.layout.action_title, null);
        tvTitle = actionbarView.findViewById(R.id.tv_title);
        relLeftImage = actionbarView.findViewById(R.id.rel_left);
        relTitleTop = actionbarView.findViewById(R.id.rel_title_top);
        imgMy = actionbarView.findViewById(R.id.img_my);
        tvRight = actionbarView.findViewById(R.id.tv_right);
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.LEFT);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(actionbarView, params);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rightTextOnclick();
            }
        });
        imgMy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                leftImageOnclick();
            }
        });
        relLeftImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 左边点击事件
     */
    public void leftImageOnclick() {
    }

    /**
     * 右边文字点击事件
     */
    public void rightTextOnclick() {
    }

    public String getRightText() {
        return tvRight.getText().toString().trim();
    }
}
